﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace PrintDrawing.BaseClassDraw
{
    internal class PageInfo
    {
        private const string PAGE_NAME = "Page";
        //for <Page/>
        private const string PAGE_NUMBER = "Number";
        private const string PAGE_BACKGROUND = "BackGround";
        private const string PAGE_HEIGHT_CURRENT = "Height_Page";
        private const string PAGE_WIDTH_CURRENT = "Width_Page";
        // DUNGPV bổ xung có đoạn set cỡ cho riêng page.
        public double HeightCurrentPage = 0;
        public double WidthCurrentPage = 0;

        int _numberPage = 0;
        public int Number
        {
            get { return _numberPage; }
            set { _numberPage = value; }
        }

        private string _background = "";
        public string Background
        {
            get { return _background; }
            set { _background = value; }
        }

        private List<NodeElement> _nodes = new List<NodeElement>();
        internal List<NodeElement> Nodes
        {
            get { return _nodes; }
            set { _nodes = value; }
        }
        public XmlNode ExportElementToXmlNode(XmlDocument doc)
        {            
            XmlNode pageNode = doc.CreateElement(PAGE_NAME);
            //attribute number
            XmlAttribute pageAttribute = doc.CreateAttribute(PAGE_NUMBER);
            pageAttribute.Value = this.Number.ToString();
            pageNode.Attributes.Append(pageAttribute);
            //attribute background
            pageAttribute = doc.CreateAttribute(PAGE_BACKGROUND);
            pageAttribute.Value = this.Background;
            pageNode.Attributes.Append(pageAttribute);
            //load height and width
            if (HeightCurrentPage != 0 && WidthCurrentPage != 0)
            {
                pageAttribute = doc.CreateAttribute(PAGE_HEIGHT_CURRENT);
                pageAttribute.Value = this.HeightCurrentPage.ToString();
                pageNode.Attributes.Append(pageAttribute);

                pageAttribute = doc.CreateAttribute(PAGE_WIDTH_CURRENT);
                pageAttribute.Value = this.WidthCurrentPage.ToString();
                pageNode.Attributes.Append(pageAttribute);
            }

            // xong pages các phần tử con của page
            foreach (NodeElement node in this.Nodes)
            {
                pageNode.
                    AppendChild(node.ExportElementToXmlNode(doc));
            }
            return pageNode;
        }

        public static List<PageInfo> ExportXmlToClass(XmlNode root)
        {
            //get root=<pages>
            List<PageInfo> pages = new List<PageInfo>();            
            ///search các node page          
            foreach (XmlNode nodePage in root.ChildNodes)
            {
                    PageInfo p = new PageInfo();
                    //d is page
                    if (nodePage.Name == PAGE_NAME)
                    {
                        /// search with attribute
                        foreach (XmlAttribute a in nodePage.Attributes)
                        {
                            if (a.Name == PAGE_BACKGROUND)
                                p.Background = a.Value;
                            if (a.Name == PAGE_NUMBER)
                                p.Number = int.Parse(a.Value);
                            if (a.Name == PAGE_HEIGHT_CURRENT)
                                p.HeightCurrentPage = double.Parse(a.Value);
                            if (a.Name == PAGE_WIDTH_CURRENT)
                                p.WidthCurrentPage = double.Parse(a.Value);                            
                        }
                        //search node with nodeElement
                        p.Nodes = NodeElement.ExportXmlToClass(nodePage);                        
                    }
                    pages.Add(p);
                
            }
            return pages;
        }
    }
}
